//console.log(userInfo);

$(function(){
    
    $.get(baseUrl + '/cart/lists', { userId: userInfo.id }, function (res) {
        let data = res.data;
        console.log(data);
        html = '';
        for (let i in data) {
            console.log(data[i]);
            html += `<tr data-goodsId="${data[i].goodsId}" data-cart="${data[i].id}" data-goodsCount="${data[i].goodsCount}">
    <td><input type="checkbox" class="j-check"></td>
        <td class="col1"><a href=""><img src="${data[i].goods.goodsImg}" alt="" /></a>  <strong><a href="">${data[i].goods.goodsName}</a></strong></td>
        <td class="col2">${data[i].goods.goodsBody} </td>
        <td class="col3">￥<span>${data[i].goods.shopPrice}</span></td>
        <td class="col4"> 
            <a href="javascript:;" class="reduce_num"></a>
            <input type="text" name="amount" value="${data[i].goodsCount}" class="amount"/>
            <a href="javascript:;" class="add_num"></a>
        </td>
        <td class="col5">￥<span>${data[i].goods.shopPrice*data[i].goodsCount}</span></td>
        <td class="col6"><a href="javascript:;" >删除</a></td>
    </tr>`
        }
        $('tbody').html(html)
      getsum();
    })

//购物车操作
    //全选全不选
    $('.checkAll').change(function () {
        $('.j-check,.checkAll').prop('checked', $(this).prop('checked'))
        getsum()
    })
    //反选
    $('tbody').on('change', '.j-check', (function () {
        if ($('.j-check:checked').length == $('.j-check').length) {
            $('.checkAll').prop('checked', true)
        } else {
            $('.checkAll').prop('checked', false)
        }
        getsum()
    }))
    //点击加减数量
    // $('.mycart ').on('click','.add_num',function(){
    //   let sum =   $(this).prev().val();
    //   $(this).prev().val( sum-0+1)
    // })
    // $('.mycart ').on('click','.reduce_num',function(){
    //     let sum =   $(this).next().val();
    //     if(sum==1){
    //         alert('至少要购买一件');
    //         return false
    //     }else{
    //         $(this).next().val( sum-1)
    //     }

    // })
    $('.mycart ').on('click', '.add_num', function () {
        let n = $(this).prev().val();
        n++
        $(this).prev().val(n);
        //获取单价
        let p = $(this).parent().siblings('.col3').find('span').text();
        // console.log(p); 
         $(this).parent().siblings('.col5').find('span').text((p * n).toFixed(2))
         $(this).parents('tr').attr('data-goodsCount',n)
         let cartId = $(this).parents('tr').attr('data-cart');
         let goodsId = $(this).parents('tr').attr('data-goodsId');
         let goodsCount = $(this).parents('tr').attr('data-goodsCount');
         
         console.log(goodsCount);

        getsum();
        //发请求
        $.post(baseUrl+'/cart/change',{userId:userInfo.id,cartId,goodsId,goodsCount},function(res){
            if(res.code != 0){
                console.log(res.msg);
            }
            console.log(res.data);
        })
    })

    $('.mycart ').on('click', '.reduce_num', function () {
        let n = $(this).next().val();
        if (n == 1) {
            alert('最少购买一件');
            return false;
        } else {
            n--
            $(this).next().val(n);
            $(this).prev().val(n);
            //获取单价
            let p = $(this).parent().siblings('.col3').find('span').text();
            // console.log(p); 
            $(this).parent().siblings('.col5').find('span').text((p * n).toFixed(2))
            $(this).parents('tr').attr('data-goodsCount',n)
            let cartId = $(this).parents('tr').attr('data-cart');
            let goodsId = $(this).parents('tr').attr('data-goodsId');
            let goodsCount = $(this).parents('tr').attr('data-goodsCount');
            $.post(baseUrl+'/cart/change',{userId:userInfo.id,cartId,goodsId,goodsCount},function(res){
                if(res.code != 0){
                    console.log(res.msg);
                }
                console.log(res.data);
            })
            getsum()
        }
    })
    //bug修复
    $('tbody').on('change', '.amount', function () {
        let n = $(this).val();
        let p = $(this).parent().siblings('.col3').find('span').text();
        // console.log(p); 
         $(this).parent().siblings('.col5').find('span').text((p * n).toFixed(2))
        getsum();
    })
    function getsum() {
        var count = 0;//计算总数量
        var money = 0;//计算总价钱
            $('.j-check').each(function (index, ele) {
                if($(this).prop('checked')){  
                    count += $(this).parents('tr').find('.amount').val()-0;
                       
                    money +=$(this).parents('tr').find('.col5>span').text()-0
                    
                }/* else{
                    $('.zj>span').text(0);
                    $('.total').text(0);
                } */
                $('.zj>span').text(count);
                $('.total').text(money.toFixed(2));
            });     
        /* $('.j-check').each(function(index,ele){
            if($(this).prop('checked')){
                money +=$(this).parents('tr').find('.col5>span').text()-0
                $('.total').text(money.toFixed(2));
            }else{
                console.log(money);
                $('.total').text(0);
            }
            
        }); */
       
    };
//购物车删除
$('tbody').on('click','.col6>a',function(){
    let cartId = $(this).parents('tr').attr('data-cart');
    $.get(baseUrl+'/cart/remove',{cartId:cartId,userId:userInfo.id},function(res){
  //console.log(res);

    })
    $(this).parents('tr').remove();
});
//购物车清空
$('.continue').click(function(){
    $.get(baseUrl+'/cart/clear',{userId:userInfo.id},function(res){

    });
    $('tbody').remove();

})
   

//跳转到flow2
    $('.checkout').click(function(){
        if ($('.zj').find('span').text()==0){
            alert('数量不能为0');
            return false;
        }else{
            location.href = 'flow2.html'
        }
      
        return false;
    })

})